package com.hxkcloud.nurse.popup;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.BrightnessUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by william_sun on 2016-07-07.
 * Description:标记时间，选择时间
 * http://blog.csdn.net/idle_thing/article/details/17645831
 */
public class CalendarPopWindow extends PopupWindow {

    private Activity context;
    private MaterialCalendarView calendar;
    private TextView txt_title;

    public CalendarPopWindow(final Activity context) {
        this.context = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.popup_cutom_calendar, null);
        setContentView(rootView);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));

        setAnimationStyle(R.style.popupwindow_anim_alpha);

        calendar = (MaterialCalendarView) rootView.findViewById(R.id.calendarView);

        // Calendar instance = Calendar.getInstance();
        // widget.setSelectedDate(instance.getTime());
        calendar.setSelectionMode(MaterialCalendarView.SELECTION_MODE_SINGLE);
        TextView txt_cancel = (TextView) rootView.findViewById(R.id.txt_cancel);
        TextView txt_sure = (TextView) rootView.findViewById(R.id.txt_sure);
        txt_title = (TextView) rootView.findViewById(R.id.txt_title);
        txt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
            }
        });
        txt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (calendar.getSelectedDate()!=null&&calendar.getSelectedDate().getDate() != null && calendarListener != null)
                    calendarListener.setCalendar((new SimpleDateFormat("yyyy-MM-dd")).format(calendar.getSelectedDate().getDate()));
                dismiss();
            }
        });
        // calendar.setTopbarVisible(false);
 /*       calendar.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                if (selected)
                calendarListener.setCalendar((new SimpleDateFormat("yyyy-MM-dd")).format(date.getDate()));
            }
        });*/
 /*     //设置日期点击监听器
        calendar.setOnDateSelectedListener(new CalendarView.OnDateSelectedListener() {

            @Override
            public void onDateUnselected(Date date) {
            }

            @Override
            public void onDateSelected(Date date) {
                calendarListener.setCalendar((new SimpleDateFormat("yyyy-MM-dd")).format(date));
                dismiss();
            }
        });*/
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
             //   calendar = null;
                BrightnessUtils.lightOn(context);
            }
        });
    }

    public void setOnDateChangedListener(OnDateSelectedListener onDateChangedListener) {
        if (calendar != null) {
            calendar.setOnDateChangedListener(onDateChangedListener);
        }
    }

    public void setTitle(String title) {
        if (txt_title != null) {
            txt_title.setVisibility(View.VISIBLE);
            txt_title.setText(title);
        }
    }


    public void setMode_multiple() {
        calendar.setSelectionMode(MaterialCalendarView.SELECTION_MODE_MULTIPLE);
    }

    /**
     * 设置标注点
     *
     * @param cals      标注点
     * @param pointType 0普通点1普通点+孕点2孕点
     */
    public void setPoints(List<Calendar> cals, int pointType) {
        if (cals != null && cals.size() > 0) {
            for (Calendar cal : cals) {
                calendar.setDateSelected(cal, true);
          /*      if (pointType == 0) {
                    calendar.setDateSelected(cal,true);
                    calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                            false, true, cals);
                } else if (pointType == 2) {
                    calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                            true, false, cals);
                } else {
                    calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                            true, true, cals);
                }*/
            }
        }

    }

    public void setPoints(List<Calendar> cals1, List<Calendar> cals2) {
        if (cals1 != null && cals1.size() > 0) {
            if (cals2 != null && cals2.size() > 0) {
                cals1.addAll(cals2);
            }
            for (Calendar cal : cals1) {
                calendar.setDateSelected(cal, true);
                // calendar.markDatesOfMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cals1, cals2);
            }
        }

    }

    /**
     * 设置多选
     *
     * @param cals
     */
    public void setCalendarss(List<Calendar> cals) {
        if (cals != null && cals.size() > 0) {
            for (Calendar cal : cals) {
                calendar.setDateSelected(cal, true);

            }
        }

    }

    public List<Calendar> getCalendarss() {
        List<Calendar> calendarList = new ArrayList<>();
        List<CalendarDay> calendarDayList = calendar.getSelectedDates();
        for (int i = 0; i < calendarDayList.size(); i++) {
            calendarList.add(calendarDayList.get(i).getCalendar());
        }
        return calendarList;
    }

    public void showDropdown(View parent) {
        clearOldSelections();
        showAsDropDown(parent, 0, 0);
        BrightnessUtils.lightOff(context);
        calendar.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (calendar != null) {
                    Calendar instance = Calendar.getInstance();
                    calendar.setDateSelected(instance.getTime(), true);
                }

            }
        }, 500);
    }

    public void showBottom(View parent) {
        clearOldSelections();
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        BrightnessUtils.lightOff(context);
        calendar.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (calendar != null) {
                    Calendar instance = Calendar.getInstance();
                    calendar.setDateSelected(instance.getTime(), true);
                }
            }
        }, 500);
    }

    public interface CalendarListener {
        void setCalendar(String calendar);
    }

    private CalendarListener calendarListener;

    public void setCalendarListener(CalendarListener calendarListener) {
        this.calendarListener = calendarListener;
    }

    public void clearOldSelections() {
        if (calendar!=null)
        calendar.clearSelection();
        // calendar.clearOldSelections();
    }
}
